const mongoose = require('mongoose');

const consumptionRecordSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'User',
    required: true
  },
  amount: {
    type: Number,
    required: true,
    min: 0
  },
  type: {
    type: String,
    enum: ['purchase', 'service', 'other'],
    required: true
  },
  description: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// 更新时间中间件
consumptionRecordSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

const ConsumptionRecord = mongoose.model('ConsumptionRecord', consumptionRecordSchema);

module.exports = ConsumptionRecord;